﻿<!DOCTYPE HTML>
<html>
<head>
  <script type="application/javascript" src="pc.js"></script>
  <script type="application/javascript" src="/tests/dom/canvas/test/captureStream_common.js"></script>
</head>
<body>
<pre id="test">
<script type="application/javascript">
  createHTML({
    bug: "1166832",
    title: "Renegotiation: verify video after renegotiation"
  });

runNetworkTest(() => {
  var test = new PeerConnectionTest();

  var h1 = new CaptureStreamTestHelper2D(50, 50);
  var canvas1 = h1.createAndAppendElement('canvas', 'source_canvas1');
  var stream1;
  var vremote1;

  var h2 = new CaptureStreamTestHelper2D(50, 50);
  var canvas2;
  var stream2;
  var vremote2;

  test.setMediaConstraints([{video: true}], []);
  test.chain.replace("PC_LOCAL_GUM", [
    function DRAW_INITIAL_LOCAL_GREEN(test) {
      h1.drawColor(canvas1, h1.green);
    },
    function PC_LOCAL_CANVAS_CAPTURESTREAM(test) {
      stream1 = canvas1.captureStream(0);
      test.pcLocal.attachLocalStream(stream1);
    }
  ]);

  test.chain.append([
    function FIND_REMOTE_VIDEO() {
      vremote1 = test.pcRemote.remoteMediaElements[0];
      ok(!!vremote1, "Should have remote video element for pcRemote");
    },
    function WAIT_FOR_REMOTE_GREEN() {
      return h1.waitForPixelColor(vremote1, h1.green, 128,
                                 "pcRemote's remote should become green");
    },
    function DRAW_LOCAL_RED() {
      // After requesting a frame it will be captured at the time of next render.
      // Next render will happen at next stable state, at the earliest,
      // i.e., this order of `requestFrame(); draw();` should work.
      stream1.requestFrame();
      h1.drawColor(canvas1, h1.red);
    },
    function WAIT_FOR_REMOTE_RED() {
      return h1.waitForPixelColor(vremote1, h1.red, 128,
                                 "pcRemote's remote should become red");
    }
  ]);

  addRenegotiation(test.chain,
    [
      function PC_LOCAL_ADD_SECOND_STREAM(test) {
        canvas2 = h2.createAndAppendElement('canvas', 'source_canvas2');
        h2.drawColor(canvas2, h2.blue);
        stream2 = canvas2.captureStream(0);

        // can't use test.pcLocal.getAllUserMedia([{video: true}]);
        // because it doesn't let us substitute the capture stream
        test.pcLocal.attachLocalStream(stream2);
      }
    ]
  );

  test.chain.append([
    function FIND_REMOTE2_VIDEO() {
      vremote2 = test.pcRemote.remoteMediaElements[1];
      ok(!!vremote2, "Should have remote2 video element for pcRemote");
    },
    function WAIT_FOR_REMOTE2_BLUE() {
      return h2.waitForPixelColor(vremote2, h2.blue, 128,
                                 "pcRemote's remote2 should become blue");
    },
    function DRAW_NEW_LOCAL_GREEN(test) {
      stream1.requestFrame();
      h1.drawColor(canvas1, h1.green);
    },
    function WAIT_FOR_REMOTE1_GREEN() {
      return h1.waitForPixelColor(vremote1, h1.green, 128,
                                 "pcRemote's remote1 should become green");
    }
  ]);

  test.run();
});

</script>
</pre>
</body>
</html>
